﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.yeepay.bank;
namespace Wwtfly.BBS.ForeApplication.WebForm.YeePay
{
	public partial class Receive : System.Web.UI.Page
    {
        private static string p1_MerId;
        private static string keyValue;
        private string r0_Cmd;
        private string r1_Code;
        private string r2_TrxId;
        private string r3_Amt;
        private string r4_Cur;
        private string r5_Pid;
        private string r6_Order;
        private string r7_Uid;
        private string r8_MP;
        private string r9_BType;
        private string rp_PayDate;
        private string hmac;

        protected void Page_Load(object sender, EventArgs e)
        {
            p1_MerId = "10000432521";// so["OnlinePay_YeePay_UserNumber"].Value.ToString(); //"10000432521";                                     // 商家ID
            keyValue = "8UPp0KE8sq73zVP370vko7C39403rtK1YwX40Td6irH216036H27Eb12792t"; //so["OnlinePay_YeePay_MD5Key"].Value.ToString();//"8UPp0KE8sq73zVP370vko7C39403rtK1YwX40Td6irH216036H27Eb12792t";  // 商家密钥
            Buy.NodeAuthorizationURL = @"https://www.yeepay.com/app-merchant-proxy/node";//  "/WebForm/YeePay/Receive.aspx" // 设置请求地址
            # region 获取易宝支付成功后的请求信息
            try
            {
                r0_Cmd = Request["r0_Cmd"].ToString().Trim();
                r1_Code = Request["r1_Code"].ToString().Trim();
                r2_TrxId = Request["r2_TrxId"].ToString().Trim();
                r3_Amt = Request["r3_Amt"].ToString().Trim();
                r4_Cur = Request["r4_Cur"].ToString().Trim();
                r5_Pid = Request["r5_Pid"].ToString().Trim();
                r6_Order = Request["r6_Order"].ToString().Trim();
                r7_Uid = Request["r7_Uid"].ToString().Trim();
                r8_MP = Request["r8_MP"].ToString().Trim();
                r9_BType = Request["r9_BType"].ToString().Trim();
                rp_PayDate = Request["rp_PayDate"].ToString().Trim();
                hmac = Request["hmac"].ToString().Trim();
            }
            catch
            {
                // PF.GoError("YeePay_Receive", 101, "支付参数获取错误", "Home_Room_OnlinePay_YeePay_Receive");
                return;
            }
            #endregion

            // 校验返回数据包
            BuyCallbackResult result = Buy.VerifyCallback(p1_MerId, keyValue, r0_Cmd, r1_Code, r2_TrxId, r3_Amt, r4_Cur, r5_Pid, r6_Order, r7_Uid, r8_MP, r9_BType, rp_PayDate, hmac);
            if (string.IsNullOrEmpty(result.ErrMsg))
            {
                if (result.R1_Code == "1")
                {
                    bool isPaySuccess = false;// WriteUserAccount(user, r6_Order, Shove._Convert.StrToDouble(result.R3_Amt, 0), "易宝支付：订单号：" + result.R6_Order + "，支付金额：" + result.R3_Amt);
                    if (result.R9_BType == "1")
                    {
                        if (isPaySuccess)
                        {
                            // Response.Redirect(Shove._Web.Utility.GetUrl() + "/Home/Room/OnlinePay/OK.aspx");
                        }
                        //else
                        //{
                        //    Response.Redirect(Shove._Web.Utility.GetUrl() + "/Home/Room/OnlinePay/Fail.aspx");
                        //}
                    }
                    else if (result.R9_BType == "2")
                    {
                        // * 如果是服务器返回或者电话支付返回(result.R9_BType==2 or result.R9_BType==3)则需要回应一个特定字符串'SUCCESS',且在'SUCCESS'之前不可以有任何其他字符输出,保证首先输出的是'SUCCESS'字符串
                        Response.Write("SUCCESS");
                    }
                }
                else
                {
                    // new Log("OnlinePay").Write("易宝支付失败!");
                }
            }
            else
            {
                // new Log("OnlinePay").Write("交易签名无效" + result.ErrMsg);
            }

        }
	}
}